#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : micah
# @time    : 2023/12/2 17:55
# @function: the script is used to do something.
# @version :

import csv
"""
不推荐写法
"""
# with open('data.csv', 'w', encoding='utf-8') as f:
#     writer = csv.writer(f)
#     writer.writerow(['id',' 年龄', '姓名'])
#     writer.writerow(['01',' 18', '百创'])
#     writer.writerow(['02',' 30', '夏洛'])
#     writer.writerow(['03',' 31', '安娜'])
#
# with open('1.csv', 'w', encoding='utf-8') as f:
#     f.write('{},{},{}'.format('id', '年龄', '姓名') + '\n')
#     f.write('{},{},{}'.format('01', '8', '百创'))

"""列表写法"""
with open('data.csv', 'w', encoding='utf-8') as csvfile:
    # 将普通的文件对象转为csv对象
    writer = csv.writer(csvfile, delimiter='-')
    # 行写入
    writer.writerow(['id', 'name', 'age'])
    writer.writerow(['10001', 'Mike', 20])
    writer.writerow(['10002', 'bob', 22])
    writer.writerow(['10003', 'Jordan', 21])
    # 多行写入
    writer.writerows(['10004', 'kad', 22], ['10005', 'hut', 33])

"""字典写入"""
with open('data.csv', 'w', encoding='utf-8', newline='') as csvfile:
    fieldnames = ['id', 'name', 'age', '']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow({'id': '10001', 'name': 'Mike', 'age': 20})
    writer.writerow({'id': '10002', 'name': 'Bob', 'age': 22})
    writer.writerow({'id': '10003', 'name': 'Jordan', 'age': 21})

